package com.coollang.smartbra.presenter;

import android.os.Environment;

import com.coollang.smartbra.activity.DeviceUpdateActivity;
import com.coollang.smartbra.model.BaseModel;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;

import java.io.File;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @Auther: Timmy
 * @Data: 2017/7/13
 * @Fuction:
 */

public class DeviceUpdateActivityPresenter extends BasePresenter<BaseModel> {

    private DeviceUpdateActivity mUpdateActivity;

    public DeviceUpdateActivityPresenter(DeviceUpdateActivity updateActivity) {
        super(BaseModel.class);
        mUpdateActivity = updateActivity;
    }

    @Override
    public void setRequestInfo() {
    }

    @Override
    protected Map<String, String> getData() {
        return null;
    }

    @Override
    protected void parsrData(BaseModel baseModel) {

    }

    /**
     * 下载文件
     */
    public void downLoadFile() {
        OkGo.get("http://sport.iuphold.cn/android01.zip")//
                .tag(this)//
                .execute(new FileCallback(Environment.getExternalStorageDirectory().toString() + "/smartbra/update/", "update.zip") {  //文件下载时，需要指定下载的文件目录和文件名
                    @Override
                    public void onSuccess(File file, Call call, Response response) {
                        // file 即为文件数据，文件保存在指定目录
                        mUpdateActivity.downloadSucceed(file.getAbsolutePath());
                    }

                    @Override
                    public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
                        //这里回调下载进度(该回调在主线程,可以直接更新ui)
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        mUpdateActivity.downloadError();
                    }
                });
    }
}
